package com.yanqu.road.dao.impl.monopolymarket.office;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.MonopolyMarketNoticeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/24 18:44
 */
public class MonopolyMarketNoticeDaoImpl extends TempDataObjectDao<MonopolyMarketNoticeData> {
    public static String getMonopolyMarketNoticeDataSql = "select * from `t_c_monopolymarket_notice` where `groupId`=? and `type`=? and `time`>?  ORDER BY `time` DESC limit ?";
    public List<MonopolyMarketNoticeData> getMonopolyMarketNoticeDataList(long groupId,int type, int time, int num) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<MonopolyMarketNoticeData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketNoticeDataSql);
            int i = 1;
            call.setLong(i++, groupId);
            call.setLong(i++, type);
            call.setInt(i++, time);
            call.setInt(i++, num);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }


    @Override
    protected boolean updateData(MonopolyMarketNoticeData MonopolyMarketNoticeData) {
        return false;
    }

    public static String insertMonopolyMarketNoticeDataSql = "insert into `t_c_monopolymarket_notice`(groupId,type,userId,pos,robedId,time) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketNoticeData monopolyMarketNoticeData) {
        monopolyMarketNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketNoticeData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketNoticeData.getType()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketNoticeData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketNoticeData.getPos()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketNoticeData.getRobedId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketNoticeData.getTime()));
        if (getDb().executeNoneQuery(insertMonopolyMarketNoticeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MonopolyMarketNoticeData parseData(ResultSet rs) throws SQLException {
        MonopolyMarketNoticeData monopolyMarketNoticeData = new MonopolyMarketNoticeData();
        monopolyMarketNoticeData.setGroupId(rs.getLong("groupId"));
        monopolyMarketNoticeData.setType(rs.getInt("type"));
        monopolyMarketNoticeData.setUserId(rs.getLong("userId"));
        monopolyMarketNoticeData.setPos(rs.getInt("pos"));
        monopolyMarketNoticeData.setRobedId(rs.getLong("robedId"));
        monopolyMarketNoticeData.setTime(rs.getInt("time"));

        return monopolyMarketNoticeData;
    }

}
